/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution390 {

    public static void main(String args[]) {
        Solution390 s = new Solution390();
        System.out.println(s.lastRemaining(9));
    }

    public int lastRemaining(int n) {
        int[] seq = new int[100];
        int idx = -1;
        while (n > 1) {
            if ((idx + 1) % 2 == 0) {
                seq[++idx] = 1;
            } else {
                seq[++idx] = n % 2;
            }
            n /= 2;
        }
        int ret = 0;
        for (int i = idx; i >= 0; i--) {
            ret = ret * 2 + seq[i];
        }
        return ret + 1;
    }

}
